var itemProcessRecord_table;
$(function(){
	itemProcessRecord_table = $('#itemProcessRecord_table_id');
	var select_itemProcessRecord_item = $("#select_itemProcessRecord_item");
	var select_itemProcessRecord_itemProductionLine = $("#select_itemProcessRecord_itemProductionLine");
	var select_itemProcessRecord_organization = $("#select_itemProcessRecord_organization");
	
	var add_itemProcessRecord_itemProcess = $("#add_itemProcessRecord_itemProcess");
	var up_itemProcessRecord_itemProcess = $("#up_itemProcessRecord_itemProcess");
	var select_itemProcessRecord_itemProcess = $("#select_itemProcessRecord_itemProcess");
	$.ajax({
		type : 'POST',
		url : '/getAllOrganization',
		async : false,
		success : function(data) {
			$.each(data, function(i, organization) {
				select_itemProcessRecord_organization.append($('<option value="' + organization.id + '">'
						+ organization.name + '</option>'));
			});
		}
	});
	select_itemProcessRecord_organization.select2();
	select_itemProcessRecord_organization.on('select2:select',function(){
		 $.ajax({
				type : 'POST',
				url : '/getAllItemByOrganization',
				data: {
					"organizationId" : select_itemProcessRecord_organization.val()
				},
				async : false,
				success : function(data) {
					select_itemProcessRecord_item.empty();
					select_itemProcessRecord_itemProductionLine.empty();
					select_itemProcessRecord_itemProcess.empty();
					add_itemProcessRecord_itemProcess.empty();
					up_itemProcessRecord_itemProcess.empty();
					$.each(data, function(i, item) {
						select_itemProcessRecord_item.append($('<option value="' + item.id + '">'
									+ item.name + '</option>'));
					});
					select_itemProcessRecord_item.select2();
					select_itemProcessRecord_item.trigger('select2:select');
				}
		  });
	   });
	  
	  select_itemProcessRecord_item.on('select2:select',function(){
		  $.ajax({
				type : 'POST',
				url : '/getAllItemProductionLineByItem',
				async : false,
				data:{
					"itemId":select_itemProcessRecord_item.val()
				},
				success : function(data) {
					add_itemProcessRecord_itemProcess.empty();
					up_itemProcessRecord_itemProcess.empty();
					select_itemProcessRecord_itemProcess.empty();
					select_itemProcessRecord_itemProductionLine.empty();
					$.each(data, function(i, itemProductionLine) {
							select_itemProcessRecord_itemProductionLine.append($('<option value="' + itemProductionLine.id + '">'
									+ itemProductionLine.name + '</option>'));
					});
					select_itemProcessRecord_itemProductionLine.select2();
					select_itemProcessRecord_itemProductionLine.trigger('select2:select');
				}
			});
	  });
	  
	  select_itemProcessRecord_itemProductionLine.on('select2:select',function(){
		  $.ajax({
				type : 'POST',
				url : '/getItemProcessByItemProductionLine',
				async : false,
				data:{
					"itemProductionLineId":select_itemProcessRecord_itemProductionLine.val()
				},
				success : function(data) {
					add_itemProcessRecord_itemProcess.empty();
					up_itemProcessRecord_itemProcess.empty();
					select_itemProcessRecord_itemProcess.empty();
					$.each(data, function(i, itemProcess) {
						add_itemProcessRecord_itemProcess.append($('<option value="' + itemProcess.id + '">'
									+ itemProcess.name + '</option>'));
						up_itemProcessRecord_itemProcess.append($('<option value="' + itemProcess.id + '">'
								+ itemProcess.name + '</option>'));
						select_itemProcessRecord_itemProcess.append($('<option value="' + itemProcess.id + '">'
								+ itemProcess.name + '</option>'));
					});
					add_itemProcessRecord_itemProcess.select2();
					up_itemProcessRecord_itemProcess.select2();
					select_itemProcessRecord_itemProcess.select2();
					itemProcessRecord_table.bootstrapTable('refresh',{pageNumber:1});
				}
			});
	  });
	select_itemProcessRecord_itemProcess.on('select2:select',function(){
		itemProcessRecord_table.bootstrapTable('refresh',{pageNumber:1});
	});
	select_itemProcessRecord_organization.trigger('select2:select'); 
	itemProcessRecord_table.bootstrapTable({
		url : "/getItemProcessRecordPage",
		striped : true, // 是否显示行间隔色

		search : true,

		//导出
		showExport : true,
        exportOptions:{
            fileName: '商品流程记录统计报表',  //文件名称设置
            worksheetName: 'sheet1',  //表格工作区名称
            tableName: '商品流程记录统计报表',
            excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
        },

		// 分页相关
		sidePagination : 'server',
		pagination : true, // 是否显示分页（*）
		pageNumber : 1, // 初始化加载第一页，默认第一页,并记录
		pageSize : 10, // 默认显示几条数据 
		pageList : [ 10, 50, 100, 1000], // 用于设置当前页面可以显示的数据条数
		queryParams : function(params) {
			var nparams = {
				'itemId' : $('#select_itemProcessRecord_item').val(),
				'itemProductionLineId' : $('#select_itemProcessRecord_itemProductionLine').val(),
				'itemProcessId' : $('#select_itemProcessRecord_itemProcess').val(),
				'search' : params.search,
				'pageNumber' : (params.offset/params.limit)+1,
				'pageSize' : params.limit
			};
			return nparams;
		},
		showRefresh : true, // 是否显示刷新按钮
		uniqueId : 'id',
		columns : [ {
			checkbox : true,
			visible : true
		// 是否显示复选框 
		}, {
			field : 'id',
			title : '流程记录编号',
			width : "10%"
		}, {
			field : 'operatorId',
			title : '操作员编号',
			width : "10%"
		}, {
			field : 'operatorNickname',
			title : '操作员昵称',
			width : "10%"
		}, {
			field : 'createtime',
			title : '创建时间',
			width : "10%"
		}, {
			field : 'itemProcess.name',
			title : '操作流程',
			width : "10%"
		}, {
			field : 'itemBatchRecord.itemProductionLine.item.name',
			title : '操作商品',
			width : "10%"
		}, {
			field : 'itemBatchRecord.id',
			title : '所属批次编号',
			width : "10%"
		}, {
			field : 'itemBatchRecord.itemProductionLine.name',
			title : '所属生产线',
			width : "10%"
		}, {
			field : 'normalQRCode.code',
			title : '绑定二维码',
			width : "20%"
		}]
	});
	
	var add_itemProcessRecord_btn = $("#add_itemProcessRecord_btn_id");
	add_itemProcessRecord_btn.click(function() {
		$("#add_itemProcessRecord_itemProcess").val($("#select_itemProcessRecord_itemProcess").val()).select2();
		$('#add_itemProcessRecord_Modal').modal("show");
	});
	var up_itemProcessRecord_btn = $("#up_itemProcessRecord_btn_id");
	up_itemProcessRecord_btn.click(function() {
		var selectRows = itemProcessRecord_table.bootstrapTable("getSelections");
		if (selectRows.length != 1) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : "错误",
				buttons : [ {
					label : '关闭',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ],
				message : '请选择一行记录'
			});
			return;
		}
		$('#up_itemProcessRecord_id').val(selectRows[0].id);
		$('#up_itemProcessRecord_itemBatchRecord').val(selectRows[0].itemBatchRecord.id);
		$('#up_itemProcessRecord_itemProcess').val(selectRows[0].itemProcess.id).select2();
		$('#up_itemProcessRecord_operatorId').val(selectRows[0].operatorId);
		$('#up_itemProcessRecord_operatorNickname').val(selectRows[0].operatorNickname);
		$('#up_itemProcessRecord_NormalQRCode').val(selectRows[0].normalQRCode!=null?selectRows[0].normalQRCode.code:'');

		$('#up_itemProcessRecord_Modal').modal("show");
	});
	var add_itemProcessRecord_submit_btn = $("#add_itemProcessRecord_submit_btn_id");
	add_itemProcessRecord_submit_btn.click(function() {
		$('#add_itemProcessRecord_Modal').modal("hide");
		var formArray = getFormDataArray($("#add_itemProcessRecord_form_id"));

		var formData = eval('({' + formArray.join(',') + '})');
		$.ajax({
			type : 'POST',
			url : '/addItemProcessRecord',
			data : formData,
			success : function(data) {
				var type;
				if (data.status == 0) {
					itemProcessRecord_table.bootstrapTable("refresh");
					type = BootstrapDialog.TYPE_SUCCESS;
				} else if (data == 1) {
					type = BootstrapDialog.TYPE_WARNING;
				} else {
					type = BootstrapDialog.TYPE_DANGER;
				}
				BootstrapDialog.show({
					type : type,
					title : "执行结果",
					buttons : [ {
						label : '关闭',
						action : function(dialogItself) {
							dialogItself.close();
						}
					} ],
					message : data.msg
				});
			}
		});
	});
	var up_itemProcessRecord_submit_btn = $("#up_itemProcessRecord_submit_btn_id");
	up_itemProcessRecord_submit_btn.click(function() {
		$('#up_itemProcessRecord_Modal').modal("hide");
		var formArray = getFormDataArray($("#up_itemProcessRecord_form_id"));

		var formData = eval('({' + formArray.join(',') + '})');
		$.ajax({
			type : 'POST',
			url : '/upItemProcessRecord',
			data : formData,
			success : function(data) {
				var type;
				if (data.status == 0) {
					itemProcessRecord_table.bootstrapTable("refresh");
					type = BootstrapDialog.TYPE_SUCCESS;
				} else if (data == 1) {
					type = BootstrapDialog.TYPE_WARNING;
				} else {
					type = BootstrapDialog.TYPE_DANGER;
				}
				BootstrapDialog.show({
					type : type,
					title : "执行结果",
					buttons : [ {
						label : '关闭',
						action : function(dialogItself) {
							dialogItself.close();
						}
					} ],
					message : data.msg
				});
			}
		});

	});
	var del_itemProcessRecord_btn = $("#del_itemProcessRecord_btn_id");
	del_itemProcessRecord_btn.click(function() {
		var selectRows = itemProcessRecord_table.bootstrapTable("getSelections");
		if (selectRows.length == 0) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : "错误",
				buttons : [ {
					label : '关闭',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ],
				message : '至少选择一行记录'
			});
			return;
		} else {
			var ids = [];
			$.each(selectRows, function(i, row) {
				ids.push(row.id);
			});

			$.ajax({
				type : 'POST',
				url : '/delItemProcessRecord',
				data : {
					"ids" : ids.join(",")
				},
				success : function(data) {
					var type;
					if (data.status == 0) {
						itemProcessRecord_table.bootstrapTable("refresh");
						type = BootstrapDialog.TYPE_SUCCESS;
					} else if (data == 1) {
						type = BootstrapDialog.TYPE_WARNING;
					} else {
						type = BootstrapDialog.TYPE_DANGER;
					}
					BootstrapDialog.show({
						type : type,
						title : "执行结果",
						buttons : [ {
							label : '关闭',
							action : function(dialogItself) {
								dialogItself.close();
							}
						} ],
						message : data.msg
					});
				}
			});
		}
	});

});
